package com.yueya.basic.dept.model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.jfinal.aop.Duang;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import com.yueya.basic.dept.service.DeptService;
import com.yueya.basic.user.model.User;
import com.yueya.common.model.Tree;

public class Dept extends Model<Dept> {

	private static final long serialVersionUID = 1734394605001868058L;

	public static Dept dao = new Dept();

	public static DeptService service = Duang.duang(DeptService.class);

	public List<Tree> toTreeList(List<Dept> list, String str, String abc) {
		Iterator<Dept> it = list.iterator();
		List<Tree> treeList = new ArrayList<Tree>();
		while (it.hasNext()) {
			Tree tr = new Tree();
			Dept mu = it.next();
			tr.setId(mu.getStr("dept_id"));
			tr.setState("open");
			if ("0".equals(mu.getStr("dept_parent_id"))) {
				tr.setIconCls("icon-wlkj-organ");
			} else {
				tr.setIconCls("icon-wlkj-dept");
			}
			// tr.setChecked(true);
			// tr.setLevel(mu.getInt("dept_level"));
			tr.setPid(mu.getStr("dept_parent_id"));
			tr.setAttributes(mu);
			tr.setText(mu.getStr("dept_name"));
			treeList.add(tr);
		}

		return treeList;
	}

	public List<Tree> toTreeList2(List<Dept> list) {
		Iterator<Dept> it = list.iterator();
		List<Tree> treeList = new ArrayList<Tree>();
		while (it.hasNext()) {
			Dept mu = it.next();
			if (isleaf2(mu.getStr("dept_id"))) {
				Tree tr = new Tree();
				tr.setId(mu.getStr("dept_id"));
				tr.setState("closed");
				if ("0".equals(mu.getStr("dept_parent_id"))) {
					tr.setIconCls("icon-wlkj-organ");
				} else {
					tr.setIconCls("icon-wlkj-dept");
				}
				// tr.setChecked(false);
				// tr.setLevel(mu.getInt("dept_level"));
				tr.setPid(mu.getStr("dept_parent_id"));
				// tr.setAttributes(mu);
				tr.setText(mu.getStr("dept_name"));
				treeList.add(tr);
			}
		}

		return treeList;
	}

	public boolean isleaf2(String pid) {
		if (User.service.getUserCount(pid) == 0 && Dept.service.getDeptNumByParentId(pid) == 0) {
			return false;
		} else if (User.service.getUserCount(pid) == 0 && Dept.service.getDeptNumByParentId(pid) != 0) {
			List<Dept> list = Dept.service.getDeptsByParentId(pid);
			Iterator<Dept> it = list.iterator();
			boolean bl = false;
			while (it.hasNext()) {
				Dept record = it.next();
				// 判断用户
				bl = isleaf2(record.getStr("dept_id"));
				if (bl) {
					break;
				}
			}

			return bl;

		} else {
			return true;
		}

	}

	public List<Tree> splitTreeU(List<Tree> rt, List<Tree> bh, List<Tree> bh2, List<Record> list) {

		bf2(bh, bh2, list);// batch+batch2
		bf(rt, bh);// root+batch
		return rt;
	}

	public void bf2(List<Tree> bh, List<Tree> lf, List<Record> list) {
		if (bh != null) {
			Iterator<Tree> itbh = bh.iterator();
			while (itbh.hasNext()) {
				Tree batch = itbh.next();
				batch.getChildren().addAll(toTreeList3(User.service.getUserListByDeptId(batch.getId()), list));
				if (lf != null) {
					for (int i = 0; i < lf.size(); i++) {
						Tree leaf = lf.get(i);
						if (batch.getId().equals(leaf.getPid())) {
							batch.getChildren().add(leaf);

						}

					}
				}
			}
		}
	}

	public void bf(List<Tree> bh, List<Tree> lf) {
		if (bh != null) {
			Iterator<Tree> itbh = bh.iterator();
			while (itbh.hasNext()) {
				Tree batch = itbh.next();
				if (lf != null) {
					for (int i = 0; i < lf.size(); i++) {
						Tree leaf = lf.get(i);
						if (batch.getId().equals(leaf.getPid())) {
							batch.getChildren().add(leaf);

						}
					}
				}
			}
		}
	}

	public void bf2(List<Tree> bh, List<Tree> lf) {
		if (bh != null) {
			Iterator<Tree> itbh = bh.iterator();
			while (itbh.hasNext()) {
				Tree batch = itbh.next();
				batch.getChildren().addAll(toTreeList3(User.service.getUserListByDeptId(batch.getId())));
				if (lf != null) {
					for (int i = 0; i < lf.size(); i++) {
						Tree leaf = lf.get(i);
						if (batch.getId().equals(leaf.getPid())) {
							batch.getChildren().add(leaf);

						}

					}
				}
			}
		}
	}

	public List<Tree> toTreeList3(List<User> list) {
		Iterator<User> it = list.iterator();
		List<Tree> treeList = new ArrayList<Tree>();
		while (it.hasNext()) {
			Tree tr = new Tree();
			User mu = it.next();
			tr.setId(mu.getStr("id"));
			tr.setState("open");
			tr.setIconCls("icon-wlkj-user_suit");
			tr.setPid(mu.getStr("dept_id"));
			// tr.setAttributes(mu);
			tr.setText(mu.getStr("user_name"));
			treeList.add(tr);
		}

		return treeList;
	}

	public List<Tree> toTreeList3(List<User> list, List<Record> listuser) {
		Iterator<User> it = list.iterator();
		List<Tree> treeList = new ArrayList<Tree>();
		while (it.hasNext()) {
			Tree tr = new Tree();
			User mu = it.next();

			for (int i = 0; i < listuser.size(); i++) {
				if (listuser.get(i).equals(mu.getStr("user_id"))) {
					tr.setChecked(true);
					break;
				}
			}

			tr.setId(mu.getStr("user_id"));
			// tr.setState("closed");
			tr.setState("open");
			tr.setIconCls("icon-wlkj-user_suit");
			// tr.setChecked(true);
			tr.setPid(mu.getStr("dept_id"));
			tr.setAttributes(mu);
			tr.setText(mu.getStr("user_name"));
			treeList.add(tr);
		}

		return treeList;
	}

	public List<Dept> _r() {
		List<Dept> r = null;
		String sql = " select  * from  t_dept where dept_parent_id = '0' ";
		r = Dept.dao.find(sql);
		return r;
	}

	/**
	 * 所属的部门同级别
	 * 
	 * @param parend_id
	 * @return
	 */
	public List<Dept> _rtl() {
		List<Dept> r = null;
		String sql = "select  * from  t_dept where del_flag = '0' order by order_id asc";
		r = Dept.dao.find(sql);
		return r;
	}
}
